package xyz.wanhongtao.smtpclient.controller;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.wanhongtao.smtpclient.object.form.UserLoginForm;
import xyz.wanhongtao.smtpclient.object.form.UserRegistryForm;
import xyz.wanhongtao.smtpclient.object.vo.Result;
import xyz.wanhongtao.smtpclient.service.UserService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

/**
 * @author wanghongtao <wanghongtao05@kuaishou.com>
 * Created on 2023-07-03
 */
@AllArgsConstructor
@RestController
@RequestMapping("/api")
public class UserController {
    private final UserService userService;

    @PostMapping("login")
    public Result<?> login(@RequestBody UserLoginForm userLoginForm, HttpServletResponse response) {
        if (userService.login(userLoginForm)) {
            Cookie cookie = new Cookie("auth", "true");
            response.addCookie(cookie);
            return Result.success(true);
        }
        return Result.success(false);
    }

    @PostMapping("registry")
    public Result<?> registry(@RequestBody UserRegistryForm userRegistryForm) {
        return Result.success(userService.registry(userRegistryForm));
    }

}
